function id(id) {
	return document.getElementById(id);
}

function tag(tag, elem) {
	return (elem || document).getElementsByTagName(tag || "*");
}

function prev(elem) {
	do{
		elem = elem.previousSibling;
	} while ( elem && elem.nodeType != 1);
	return elem;
}

function next(elem) {
	do{
		elem = elem.nextSibling;
	}	while (elem && elem.nodeType != 1);
	return elem;
}

function first(elem) {
	elem = elem.firstChild;
	return elem && elem.nodeType != 1 ? next(elem) : elem;
}

function last(elem) {
	elem = elem.lastChild;
	return elem && elem.nodeType != 1 ? prev(elem) : elem;
}

function parent(elem, level) {
	level = level || 1;
	for (var i=0; i<level ; i++ )
		if (elem)	elem = elem.parentNode;
	return elem;
}

function children(elem) {
	var r = [];
	elem = first(elem);
	if (!elem)	return r;
	do{
		r.push(elem);
	}
	while (elem = next(elem));
	return r;
}

function text(elem) {
	var t = "";
	elem = elem.childNodes || elem;
	for (var i=0; i<elem.length ; i++ ){
		t += elem[i].nodeType != 1 ? elem[i].nodeValue : text(elem[i].childNodes);
	}
	return t;
}

function val(elem, value) {
	return attr(elem, 'value', value);
}

function html(elem, value) {
	if(typeof value == "string")
		elem.innerHTML = value;
	else
		return elem.innerHTML;
}

function attr(elem, name, value) {
	if(! name || name.constructor != String)	 return '';
	name = {'for': 'htmlFor', 'class': 'className'}[name] || name;
	if(typeof value != "undefined") {
		elem[name] = value;
		if (elem.setAttribute)	elem.setAttribute(name, value);
	}
	return elem[name] || elem.getAttribute(name) || '';
}

function create(tag) {
	if (tag.charAt(0) == '<'){
		var elem = create("div");
		elem.innerHTML = tag;
		return first(elem);
	} else {
		return document.createElementNS ? document.createElementNS('http://www.w3.org/1999/xhtml', tag) : document.createElement(tag);
	}
}

function before(elem, insert) {
	parent(elem).insertBefore(insert, elem);
}

function append(elem, insert) {
	elem.appendChild(insert);
}

function remove(elem) {
	if (elem)	parent(elem).removeChild(elem);
}

function position(elem) {
	position.px = function(elem) {
		return elem.offsetParent ? elem.offsetLeft + position.px(elem.offsetParent) : elem.offsetLeft;
	}
	position.py = function(elem) {
		return elem.offsetParent ? elem.offsetTop + position.py(elem.offsetParent) : elem.offsetTop;
	}
	return {"x": position.px(elem), "y": position.py(elem)};
}

function pageWidth() {
	return width(document.body);
}

function pageHeight() {
	return height(document.body);
}

function windowWidth() {
	return self.innerWidth ||
		(document.documentElement && document.documentElement.clientWidth) ||
		document.body.clientWidth;
}

function windowHeight() {
	return self.innerHeight ||
		(document.documentElement && document.documentElement.clientHeight) ||
		document.body.clientHeight;
}

function scrollX() {
	return self.pageXOffset ||
		(document.documentElement && document.documentElement.scrollLeft) ||
		document.body.scrollLeft;
}

function scrollY() {
	return self.pageYOffset ||
		(document.documentElement && document.documentElement.scrollTop) ||
		document.body.scrollTop;
}